package org.jeecg.modules.exChargeSetting.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.exChargeSetting.entity.ExChargeSetting;
import org.jeecg.modules.exChargeSetting.service.IExChargeSettingService;
import org.jeecg.modules.exChargeSetting.vo.ExChargeSettingPage;
import org.jeecg.modules.exProject.constant.ExProjectStepEnum;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.jeecg.modules.exProject.vo.ExProjectParam;
import org.jeecg.modules.gcProject.vo.StatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 资费设置
 * @Author: jeecg-boot
 * @Date: 2024-09-03
 * @Version: V1.0
 */
@Api(tags = "资费设置")
@RestController
@RequestMapping("/exChargeSetting/exChargeSetting")
@Slf4j
public class ExChargeSettingController extends JeecgController<ExChargeSetting, IExChargeSettingService> {
    @Autowired
    private IExChargeSettingService exChargeSettingService;
    @Autowired
    private IExSectionService iExSectionService;
    @Autowired
    private IExProjectService iExProjectService;

    /**
     * 分页列表查询
     *
     * @param exChargeSetting
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "资费设置-分页列表查询")
    @ApiOperation(value = "资费设置-分页列表查询", notes = "资费设置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ExChargeSetting>> queryPageList(ExChargeSetting exChargeSetting,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<ExChargeSetting> queryWrapper = QueryGenerator.initQueryWrapper(exChargeSetting, req.getParameterMap());
        Page<ExChargeSetting> page = new Page<ExChargeSetting>(pageNo, pageSize);
        IPage<ExChargeSetting> pageList = exChargeSettingService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param exChargeSetting
     * @return
     */
    @AutoLog(value = "资费设置-添加")
    @ApiOperation(value = "资费设置-添加", notes = "资费设置-添加")
    //@RequiresPermissions("org.jeecg.modules:ex_charge_setting:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ExChargeSetting exChargeSetting) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExChargeSetting byProjectId = exChargeSettingService.getByProjectId(exChargeSetting.getProjectId());
        if (ObjectUtil.isNotEmpty(byProjectId))
            throw new JeecgBootException("该项目已存在资费设置");
        exChargeSetting.setCreateBy(sysUser.getId());
        exChargeSetting.setCreateTime(new Date());
        exChargeSetting.setStatus(StatusEnum.SAVE.getCode());
        exChargeSettingService.save(exChargeSetting);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param exChargeSetting
     * @return
     */
    @AutoLog(value = "资费设置-编辑")
    @ApiOperation(value = "资费设置-编辑", notes = "资费设置-编辑")
    //@RequiresPermissions("org.jeecg.modules:ex_charge_setting:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ExChargeSetting exChargeSetting) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        exChargeSetting.setCreateBy(sysUser.getId());
        exChargeSetting.setUpdateTime(new Date());
        exChargeSettingService.updateById(exChargeSetting);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "资费设置-编辑、新增")
    @ApiOperation(value = "资费设置-编辑、新增", notes = "资费设置-编辑、新增")
    @RequestMapping(value = "/save", method = {RequestMethod.PUT, RequestMethod.POST})
    @Transactional(rollbackFor = Exception.class)
    public Result<ExChargeSetting> save(@RequestBody ExChargeSettingPage exChargeSettingPage) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExChargeSetting exChargeSetting = new ExChargeSetting();
        BeanUtils.copyProperties(exChargeSettingPage, exChargeSetting);
        if (StringUtils.isNotBlank(exChargeSettingPage.getId())) {
            exChargeSetting.setUpdateBy(sysUser.getUsername());
            exChargeSetting.setUpdateTime(new Date());
            exChargeSettingService.updateById(exChargeSetting);
        } else {
            ExChargeSetting byProjectId = exChargeSettingService.getByProjectId(exChargeSetting.getProjectId());
            if (ObjectUtil.isNotEmpty(byProjectId))
                throw new JeecgBootException("该项目已存在资费设置");
            if (StringUtils.isEmpty(exChargeSetting.getStatus())) {
                exChargeSetting.setStatus(StatusEnum.SAVE.getCode());
            }
            exChargeSetting.setCreateBy(sysUser.getUsername());
            exChargeSetting.setCreateTime(new Date());
            exChargeSettingService.save(exChargeSetting);
        }
        iExSectionService.updateBatchById(exChargeSettingPage.getExSectionList());
        return Result.OK("操作成功！", exChargeSettingService.getById(exChargeSetting.getId()));
    }

    @AutoLog(value = "资费设置-审核")
    @ApiOperation(value = "资费设置-审核", notes = "资费设置-审核")
    @PostMapping(value = "/audit")
    public Result<String> audit(@RequestBody ExProjectParam exProjectParam) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExChargeSetting exChargeSetting = exChargeSettingService.getById(exProjectParam.getId());
        if (exChargeSetting == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        exChargeSetting.setStatus(exProjectParam.getStatus());
        exChargeSetting.setUpdateTime(new Date());
        exChargeSetting.setUpdateBy(sysUser.getUsername());
        exChargeSettingService.updateById(exChargeSetting);
        // 审核通过则更新项目信息的步骤为资费定义
        ExProject exProject = iExProjectService.getById(exChargeSetting.getProjectId());
        if (null != exProject && exProject.getStep() < ExProjectStepEnum.ZFDD.getValue() && StatusEnum.SUCCESS.getCode().equals(exProjectParam.getStatus())) {
            exProject.setStep(ExProjectStepEnum.ZFDD.getValue());
            iExProjectService.updateById(exProject);
        }
        return Result.OK("审核成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资费设置-通过id删除")
    @ApiOperation(value = "资费设置-通过id删除", notes = "资费设置-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:ex_charge_setting:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        exChargeSettingService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "资费设置-批量删除")
    @ApiOperation(value = "资费设置-批量删除", notes = "资费设置-批量删除")
    //@RequiresPermissions("org.jeecg.modules:ex_charge_setting:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.exChargeSettingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "资费设置-通过id查询")
    @ApiOperation(value = "资费设置-通过id查询", notes = "资费设置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ExChargeSetting> queryById(@RequestParam(name = "id", required = true) String id) {
        ExChargeSetting exChargeSetting = exChargeSettingService.getById(id);
        if (exChargeSetting == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(exChargeSetting);
    }

    @ApiOperation(value = "资费设置-通过projectId查询", notes = "资费设置-通过projectId查询")
    @GetMapping(value = "/queryByProjecrtId")
    public Result<ExChargeSetting> queryByProjecrtId(@RequestParam(name = "projectId", required = true) String projectId) {
        return Result.OK(this.exChargeSettingService.getByProjectId(projectId));
    }

    /**
     * 导出excel
     *
     * @param request
     * @param exChargeSetting
     */
    //@RequiresPermissions("org.jeecg.modules:ex_charge_setting:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExChargeSetting exChargeSetting) {
        return super.exportXls(request, exChargeSetting, ExChargeSetting.class, "资费设置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("ex_charge_setting:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExChargeSetting.class);
    }

}
